package com.industrial.premu.service.impl;

import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.vo.CompanyPolicyVo;
import com.industrial.premu.domain.vo.DifferentiatedPolicySimulationVo;
import com.industrial.premu.mapper.DifferentiatedPolicySimulationMapper;
import com.industrial.premu.service.IDifferentiatedPolicySimulationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DifferentiatedPolicySimulationServiceImpl implements IDifferentiatedPolicySimulationService {

    @Autowired
    private DifferentiatedPolicySimulationMapper differentiatedPolicySimulationMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * <p>
     * 查询列表
     * </p>
     *
     * @param params
     * @return
     */
    @Override
    public List<DifferentiatedPolicySimulationVo> list(DifferentiatedPolicySimulationVo params) {
        List<DifferentiatedPolicySimulationVo> result = differentiatedPolicySimulationMapper.list(params);
        for(DifferentiatedPolicySimulationVo temp : result){

            StringBuilder area = new StringBuilder();

            String releaseProvince = temp.getReleaseProvince();
            String releaseCity = temp.getReleaseCity();
            String releaseCounty = temp.getReleaseCounty();

            if(StringUtils.isNotBlank(releaseProvince)){
                area.append(redisTemplate.opsForValue().get("area:" + releaseProvince));
            }

            if(StringUtils.isNotBlank(releaseCity)){
                area.append(redisTemplate.opsForValue().get("area:" + releaseCity));
            }

            if(StringUtils.isNotBlank(releaseCounty)){
                area.append(redisTemplate.opsForValue().get("area:" + releaseCounty));
            }

            temp.setArea(area.toString());
        }
        return result;
    }

    /**
     * @param preMuId
     * @return
     */
    @Override
    public PreMuBenefitEvaluation queryInfo(String preMuId) {
        PreMuBenefitEvaluation baseInfo = differentiatedPolicySimulationMapper.queryCompanyPreMuInfo(preMuId);
        List<CompanyPolicyVo> r =  differentiatedPolicySimulationMapper.selectCompanyJoinPolicy(baseInfo);
        Map<String, List<CompanyPolicyVo>> collect = new HashMap<>();
        if(r != null && !r.isEmpty()){
            collect = r.stream().collect(Collectors.groupingBy(CompanyPolicyVo::getPolicyName));
        }
        baseInfo.setCompanyPolicyMap(collect);
        return baseInfo;
    }
}
